﻿namespace WpfDemoControlLibrary
{
    using System.Windows;
    using System.Windows.Controls;

    /// <summary>
    /// Interaction logic for PersonUserControl.xaml
    /// </summary>
    public partial class PersonUserControl : UserControl
    {
        public PersonUserControl() : this(new PersonUserControlViewModel())
        {
            
        }

        public PersonUserControl(PersonUserControlViewModel dataContext)
        {
            InitializeComponent();
            // Set ViewModel as data context.
            this.DataContext = dataContext;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            // Add a new person to the collection of persons in the view model.
            Person person = new Person();
            person.Name = txtName.Text;
            person.Firstname = txtFirstname.Text;
            person.Nationality = cmbNationality.SelectedValue.ToString();
            ((PersonUserControlViewModel)this.DataContext).Persons.Add(person);
        }
    }
}
